package mBio;

import java.io.*;
import java.util.*;
import rfBio.*;


public class RemoveOverlapsInfile {

	public static void main(String[] args) 
						throws IOException, FileNotFoundException {
		// local variables
		blastm8 ref = new blastm8();
		blastm8 nov = new blastm8();
		blastm8 tempblast = new blastm8();
		Vector refVector = new Vector(100, 100);
		boolean addFlag = true;
		String o_filename = new String();
		String no_filename = new String();
		String o_filenametab = new String();
		String no_filenametab = new String();
		
		// ***************************************************
		// read file section
		// ***************************************************
		
		// open ref_file
		BufferedReader refFile = new BufferedReader ( new FileReader (args[0]));
		
		// read ref_file line by line
		while (true){
			String line = refFile.readLine();
			if (line == null)
			{
				break;
			}			
			
			StringTokenizer ltok = new StringTokenizer(line, "\t");
			while ( ltok.hasMoreTokens() ){
				ref.set_query(ltok.nextToken());
				ref.set_subject(ltok.nextToken());
				ref.set_p1(Type.toFloat( ltok.nextToken() ));
				ref.set_allength(Type.toInt( ltok.nextToken() ));
				ref.set_p2(Type.toInt( ltok.nextToken() ));
				ref.set_p3(Type.toInt( ltok.nextToken() ));
				ref.set_qstart(Type.toInt( ltok.nextToken() ));
				ref.set_qstop(Type.toInt( ltok.nextToken() ));
				ref.set_sstart(Type.toInt( ltok.nextToken() ));
				ref.set_sstop(Type.toInt( ltok.nextToken() ));
				ref.set_evalue(Type.toFloat( ltok.nextToken() ));
				ref.set_score(Type.toInt( ltok.nextToken() ));		
			}
			
			// check if better score is already in file
			for(int i=0; i < refVector.size(); i++){
				tempblast = (blastm8) refVector.get(i);
				if( (tempblast.get_score() > ref.get_score()) && (tempblast.get_query().equals(ref.get_query()) )){
					addFlag = false;
				}
			}
			if(addFlag = true){
				
				// remove all other hits with same name
				for(int j= 0; j < refVector.size(); j++){
					tempblast = (blastm8) refVector.get(j);
					
					if(tempblast.get_query().equals(ref.get_query())){
						refVector.remove(j);
					}
				}
				refVector.add(new blastm8(ref));
			}
			addFlag = true;
		}

		// close ref_ file
		refFile.close();						
													
		
		// iterate over novVector 
		for( int i=0; i < refVector.size(); i++)
		{
			nov = (blastm8) refVector.get(i);
			
			// iterate over refVector 
			for( int j=i+1; j < refVector.size(); j++){
				ref = (blastm8) refVector.get(j);
				//check if hit same sequence
				if( ref.get_subject().equals( nov.get_subject() ) ){
					//check if nov overlaps ref_start, if so mark
					if( (nov.get_sstart() <= ref.get_sstart()) && (nov.get_sstop() >= ref.get_sstart()) ){
						nov.set_overlaps_toTrue();
						nov.set_overlaps_with(ref.get_query());
						refVector.set(i,nov);
					}
					//check if nov overlaps ref_end, if so mark
					if( (nov.get_sstart() <= ref.get_sstop()) && (nov.get_sstop() >= ref.get_sstop()) ){
						nov.set_overlaps_toTrue();
						nov.set_overlaps_with(ref.get_query());
						refVector.set(i,nov);
						//System.out.println("3");
					}
					//check if start of nov is between ends of ref_start, if so mark
					if( (ref.get_sstart() <= nov.get_sstart()) && (ref.get_sstart() >= nov.get_sstop()) ){
						nov.set_overlaps_toTrue();
						nov.set_overlaps_with(ref.get_query());
						refVector.set(i,nov);
					}
					
					//check if end of nov is between ends of ref_start, if so mark
					if( (ref.get_sstart() <= nov.get_sstop()) && (ref.get_sstop() >= nov.get_sstop()) ){
						nov.set_overlaps_toTrue();
						nov.set_overlaps_with(ref.get_query());
						refVector.set(i,nov);
					}
				}

			}
		}
		
		//write output files
		o_filename = args[1] + "_overlaps.txt";
		no_filename = args[1] + "_nonoverlaps.txt";
		o_filenametab = args[1] + "_overlaps.tab";
		no_filenametab = args[1] + "_nonoverlaps.tab";
		
		PrintWriter o_file = new PrintWriter(new BufferedWriter(new FileWriter(o_filename)));
		PrintWriter no_file = new PrintWriter(new BufferedWriter(new FileWriter(no_filename)));
		PrintWriter o_filetab = new PrintWriter(new BufferedWriter(new FileWriter(o_filenametab)));
		PrintWriter no_filetab = new PrintWriter(new BufferedWriter(new FileWriter(no_filenametab)));
		
		for(int i =0; i < refVector.size(); i++)
		{
			nov = (blastm8) refVector.get(i);
			if(nov.get_overlaps()){
				o_file.println( nov.get_query() + " overlaps with " + nov.get_overlaps_with());
				o_filetab.println(nov.get_query());
			}
			else{
				no_file.println( nov.get_query() + " does not overlap any other sequence in the file.");
				no_filetab.println(nov.get_query());
			}
		}
		
		o_file.close();
		no_file.close();
		o_filetab.close();
		no_filetab.close();
		
	} // end main

}
